﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace WebAppFilterDemo.FilterExt
{
    ///// <summary>
    ///// 无参构造函数
    ///// </summary>
    //public class ExecptionFilter : Attribute, IExceptionFilter
    //{
    //    //private ILogger<ExecptionFilter> _logger;


    //    ////构造注入日志组件
    //    //public ExecptionFilter(ILogger<ExecptionFilter> logger)
    //    //{
    //    //    _logger = logger;
    //    //}


    //    public void OnException(ExceptionContext context)
    //    {
    //        //日志收集
    //        //_logger.LogError(context.Exception, context?.Exception?.Message ?? "异常");


    //        Console.WriteLine("ExecptionFilter - OnException ");
    //    }
    //}


    /// <summary>
    /// 有参构造函数
    /// </summary>
    public class ExecptionFilter : Attribute, IExceptionFilter
    {
        private ILogger<ExecptionFilter> _logger;


        //构造注入日志组件
        public ExecptionFilter(ILogger<ExecptionFilter> logger)
        {
            _logger = logger;
            Console.WriteLine(" ExecptionFilter  构造函数执行了");
        }


        public void OnException(ExceptionContext context)
        {
            //日志收集
            _logger.LogError(context.Exception, context?.Exception?.Message ?? "异常");


            Console.WriteLine("ExecptionFilter - OnException ");
        }
    }
}
